package netty.client;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Client {
    private static String host = "127.0.0.1";
    private static int port = 7878;
    public static Date sTime;
    public static long messageNum = 0;

    public static String[] getText() {
        //File f = new File("/home/luoqifei/Downloads/652103001.txt");
        File f = new File("/home/luoqifei/Downloads/650124033.txt");
        long len = f.length();
        byte[] bytes = new byte[(int) len];
        DataInputStream inData = null;
        try {
            inData = new DataInputStream(new FileInputStream(f));
            inData.readFully(bytes);
            inData.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //get the file all content
        String content = new String(bytes);
        Pattern variablePattern = Pattern.compile("\\n");
        Matcher variableMatcher = variablePattern.matcher(content);
        if (variableMatcher.find()) {
            String[] variableStrs = variableMatcher.group().split("\\s+");
            for (String a : variableStrs) {
                System.out.println(a);
            }
            return variableStrs;
        }
        return null;
    }

    public static List<String> getTextLine() {
        File f = new File("/home/luoqifei/Downloads/652103001.txt");
        //File f = new File("/home/luoqifei/Downloads/650124033.txt");
        List<String> text = new ArrayList<String>();
        BufferedReader bre = null;
        try {
            bre = new BufferedReader(new FileReader(f));
            String a = "";
            while ((a = bre.readLine()) != null) {
                //System.out.println(a);
                text.add(a);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bre != null) {
                try {
                    bre.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return text;
    }

    public static String getTextStr() {
        return "652103,652103001,GOLDWIND-1500-2012028,1500-2012028-A1,2015-12-30 00:00:03.654,1.840000,2.240000,527.349976,-0.720000,-0.010000,143.589996,143.589996,143.589996,8712.000000,176.070007,189.279999,5.460000,5.240000,5.360000,5.460000,0.020000,0.000000,0.020000,0.000000,-2.700000,-2.400000,-1.390000,-3.700000,-2.200000,-1.700000,-1.520000,357.179993,354.250000,354.739990,-2.630000,0.000000,0.000000,-8.540000,50.040001,3.410000,0.000000,16.000000,3.790000,-4.000000,0.000000,-2.790000,5.400000,4.800000,3.790000,4.400000,3.900000,4.000000,7.900000,8.500000,7.300000,1.100000,0.500000,1.290000,7.900000,8.500000,7.300000,58.740002,59.270000,58.590000,29.110001,29.580000,29.780001,0.550000,0.640000,-0.050000,5.920000,5.980000,6.010000,0.120000,0.150000,0.190000,24719.679688,24312.140625,388.109985,,15171.179688,10340413.000000,22152.000000,10.060000,401.619995,27.120001,27.030001,26.930000,26.780001,26.680000,26.730000,26.879999,26.780001,26.830000,26.780001,26.879999,27.120001,0.800000,19.600000,-1.700000,-1.890000,-2.200000,7.690000,6.090000,497.320007,-505.190002,2.190000,3.250000,344.190002,0.000000,0.000000,0.000000,0.000000,16.000000,4,600,0,0,0,14618.769531,7746.979980,36.119999,6.390000,7895.089844,159.800003,0.000000,12886.700195,0,0.000000,0.000000,0.000000,0.000000,0.000000,0,0,0,192.649994\n";
    }

    /**
     * @param args
     * @throws InterruptedException
     * @throws IOException
     */
    public static void main(String[] args) throws InterruptedException, IOException {
        if(args.length<3){
            System.err.println("use: socket host , port, sendMessage number");
            System.exit(1);
        }
        host = args[0];
        port = Integer.valueOf(args[1]);
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ClientInitializer());
            // connect to server
            Channel ch = b.connect(host, port).sync().channel();
            // send message to server
                /*
                 * send to server and set text end with "\r\n" for we and DelimiterBasedFrameDecoder in initializer
                 * DelimiterBasedFrameDecoder decoder by \n
                 * */
            String testContent = getTextStr();//15653  30Mb
            System.out.println("String message length :" + getTextStr().length());
            System.out.println("String message length :" + getTextStr().length());
            messageNum = Integer.valueOf(args[2]);
            System.out.println("we will send  :" + messageNum +" messages after 5 seconds.");
            sTime = new Date();
            Thread.sleep(5000);
            System.out.println("start to send data time  is :" + sTime);
            for (int i = 0; i < messageNum; i++) {
                ch.writeAndFlush(testContent);
            }
            Thread.sleep(4000000);
        } finally {
            // The connection is closed automatically on shutdown.
            group.shutdownGracefully();
        }
    }
}